// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MisinkoMaster

//@version=6
indicator("True Baseline Median SuperTrend", "TBM SuperTrend | MisinkoMaster", overlay = true, behind_chart = false)
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Import Libraries
import TradingView/ta/12
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Get User Defined Inputs
src = input.source(close, "Source",
     tooltip = "Changes the source of the baseline for the SuperTrend",
     group = "True Baseline Median SuperTrend | MisinkoMaster")

alen = input.int(27, "ATR Length", step = 1, minval = 2,
     tooltip = "Changes the lookback period for the ATR calculation.",
     group = "True Baseline Median SuperTrend | MisinkoMaster")
blen = input.int(21, "True Baseline Length", step = 1, minval = 2,
     tooltip = "Changes the period in which the baseline is calculated",
     group = "True Baseline Median SuperTrend | MisinkoMaster")

fac = input.float(1.75, "Factor", step = 0.05, minval = 0,
     tooltip = "Changes the multiplier of volatility.",
     group = "True Baseline Median SuperTrend | MisinkoMaster")

smooth = input.int(14, "Median Period", step = 1, minval = 2,
     tooltip = "Changes the lookback period of the median",
     group = "True Baseline Median SuperTrend | MisinkoMaster")
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Calculations
tr = ta.tr(true)

atr = ta.atr(alen)
median_atr = ta.median(atr, blen)

sum = 0.0
samples = 0

for i = 0 to blen-1
    if tr[i] >= median_atr
        sum += src[i]
        samples += 1

baseline = sum/samples

upper_ = baseline + atr*fac
lower_ = baseline - atr*fac

upper = ta.median(upper_, smooth)
lower = ta.median(lower_, smooth)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Trend Logic
var trend = 0

L = src > upper
S = src < lower

if L and not S
    trend := 1
if S
    trend := -1
///////////////////////////////////////////////////////////////////////////////////////////
//Plotting
upSeries   = trend == 1  ? lower : na
downSeries = trend == -1 ? upper : na

var col = color.rgb(72, 72, 72)
if trend == 1
    col := color.rgb(0, 255, 187)
if trend == -1
    col := color.rgb(255, 0, 157)
a = plot(upSeries,   "Upper", color = color.rgb(0, 255, 187), style = plot.style_linebr)
b = plot(downSeries, "Lower", color = color.rgb(255, 0, 157), style = plot.style_linebr)
c = plot(close, display = display.none, editable = false)

fill(a, c, color.rgb(0, 255, 187, 50))
fill(b, c, color.rgb(255, 0, 157, 50))

plotshape(trend > trend[1] ? upSeries : na, style = shape.labelup, location = location.absolute, size = size.normal, color = color.rgb(0, 255, 187), text = "𝓛𝓸𝓷𝓰", textcolor = color.rgb(7, 83, 63))
plotshape(trend < trend[1] ? downSeries : na, style = shape.labeldown,location = location.absolute, size = size.normal,color = color.rgb(255, 0, 157), text = "𝓢𝓱𝓸𝓻𝓽", textcolor = color.rgb(146, 11, 94))

plotcandle(open, high, low, close, color = col, bordercolor = col, wickcolor = col)
